package com.ruoyi.web.controller.statistics;

import java.text.DecimalFormat;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UnitUtils;
import com.ruoyi.statistics.domain.StatisticsSupport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.statistics.domain.StatisticsApplication;
import com.ruoyi.statistics.service.IStatisticsApplicationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目应用 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-11-11
 */
@Controller
@RequestMapping("/statistics/statisticsApplication")
public class StatisticsApplicationController extends BaseController
{
    private String prefix = "statistics/statisticsApplication";
	
	@Autowired
	private IStatisticsApplicationService statisticsApplicationService;
	
	@RequiresPermissions("statistics:statisticsApplication:view")
	@GetMapping()
	public String statisticsApplication( ModelMap mmap)
	{
		double chulierweirate = 0.0D;
		double chulisanweirate = 0.0D;
		double jieshierweirate = 0.0D;
		double jieshisanweirate = 0.0D;

		double tempCount1=0.0D;
		double tempCount2=0.0D;

		double tempCount3=0.0D;
		double tempCount4=0.0D;

		double tempCount5=0.0D;
		double tempCount6=0.0D;

		double tempCount7=0.0D;
		double tempCount8=0.0D;

		List<StatisticsApplication> list = statisticsApplicationService.selectStatisticsApplicationList(new StatisticsApplication());
		for (int i = 0; i < list.size(); i++) {

			StatisticsApplication statisticsApplication = list.get(i);

			String all_d2_area = statisticsApplication.getAllD2Area();
			if (StringUtils.isNotEmpty(all_d2_area)){
				if (statisticsApplication.getType().contains("处理")){
					tempCount1+=Double.parseDouble(all_d2_area);
				}else{
					tempCount5+=Double.parseDouble(all_d2_area);
				}
			}

			String d2_area = statisticsApplication.getD2Area();
			if (StringUtils.isNotEmpty(d2_area)){
				if(statisticsApplication.getType().contains("处理")){
					tempCount2+=Double.parseDouble(d2_area);
				}else{
					tempCount6+=Double.parseDouble(d2_area);
				}
			}

			String all_d3_area = statisticsApplication.getAllD3Area();
			if (StringUtils.isNotEmpty(all_d3_area)){
				if(statisticsApplication.getType().contains("处理")){
					tempCount3+=Double.parseDouble(all_d3_area);
				}else{
					tempCount7+=Double.parseDouble(all_d3_area);
				}
			}

			String d3_area = statisticsApplication.getD3Area();
			if (StringUtils.isNotEmpty(d3_area)){
				if(statisticsApplication.getType().contains("处理")) {
					tempCount4+=Double.parseDouble(d3_area);
				}else{
					tempCount8+=Double.parseDouble(d3_area);
				}
			}

		}

		if (tempCount2 == 0.0D) {
			chulierweirate = 0.0D;
		} else {
			chulierweirate = tempCount1 / tempCount2;
		}
		if (tempCount4 == 0.0D) {
			chulisanweirate = 0.0D;
		} else {
			chulisanweirate = tempCount3 / tempCount4;
		}

		if (tempCount6 == 0.0D) {
			jieshierweirate = 0.0D;
		} else {
			jieshierweirate = tempCount5 / tempCount6;
		}
		if (tempCount8 == 0.0D) {
			jieshisanweirate = 0.0D;
		} else {
			jieshisanweirate = tempCount7 / tempCount8;
		}

		mmap.put("chulierweirate", new DecimalFormat("#.00").format(chulierweirate * 100.0D) + "%");
		mmap.put("chulisanweirate", new DecimalFormat("#.00").format(chulisanweirate * 100.0D) + "%");
		mmap.put("jieshierweirate", new DecimalFormat("#.00").format(jieshierweirate * 100.0D) + "%");
		mmap.put("jieshisanweirate", new DecimalFormat("#.00").format(jieshisanweirate * 100.0D) + "%");

		String chuli_count = "";
		String jieshi_count = "";
		String chuli_erwei = "";String chuli_sanwei = "";
		String jieshi_erwei = "";String jieshi_sanwei = "";
		for (int i = 1; i < 20; i++)
		{
			StatisticsApplication statisticsApplication = new StatisticsApplication();
			statisticsApplication.setUnitid(String.valueOf(i));
			statisticsApplication.setType("处理");
			List<StatisticsApplication> list1 = statisticsApplicationService.selectStatisticsApplicationList(statisticsApplication);
			int count1 = 0;
			int count3 = 0;
			int count5 = 0;
			if ((list1 != null) && (list1.size() > 0))
			{
				StatisticsApplication applicationNew = (StatisticsApplication)list1.get(0);
				count1 = Integer.parseInt(applicationNew.getAllD2Count().trim()) + Integer.parseInt(applicationNew.getAllD3Count().trim());
				count3 = Integer.parseInt(applicationNew.getAllD2Area().trim());
				count5 = Integer.parseInt(applicationNew.getAllD3Area().trim());
			}
			chuli_count = chuli_count + count1 + ",";
			chuli_erwei = chuli_erwei + count3 + ",";
			chuli_sanwei = chuli_sanwei + count5 + ",";

			StatisticsApplication statisticsApplication1 = new StatisticsApplication();
			statisticsApplication1.setUnitid(String.valueOf(i));
			statisticsApplication1.setType("解释");
			List<StatisticsApplication> list2 = statisticsApplicationService.selectStatisticsApplicationList(statisticsApplication1);
			int count2 = 0;
			int count4 = 0;
			int count6 = 0;
			if ((list2 != null) && (list2.size() > 0))
			{
				StatisticsApplication applicationNew = (StatisticsApplication)list2.get(0);
				count2 = Integer.parseInt(applicationNew.getAllD2Count().trim()) + Integer.parseInt(applicationNew.getAllD3Count().trim());
				count4 = Integer.parseInt(applicationNew.getAllD2Area().trim());
				count6 = Integer.parseInt(applicationNew.getAllD3Area().trim());
			}
			jieshi_count = jieshi_count + count2 + ",";
			jieshi_erwei = jieshi_erwei + count4 + ",";
			jieshi_sanwei = jieshi_sanwei + count6 + ",";

		}
		mmap.put("chuli_counts", chuli_count.substring(0, chuli_count.length()-1));
		mmap.put("jieshi_counts", jieshi_count.substring(0,jieshi_count.length()-1));
		mmap.put("chuli_erwei_counts", chuli_erwei.substring(0, chuli_erwei.length()-1));
		mmap.put("chuli_sanwei_counts", chuli_sanwei.substring(0,chuli_sanwei.length()-1));
		mmap.put("jieshi_erwei_counts", jieshi_erwei.substring(0, jieshi_erwei.length()-1));
		mmap.put("jieshi_sanwei_counts", jieshi_sanwei.substring(0, jieshi_sanwei.length()-1));

		return prefix + "/statisticsApplication";
	}
	
	/**
	 * 查询项目应用列表
	 */
	@RequiresPermissions("statistics:statisticsApplication:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(StatisticsApplication statisticsApplication)
	{
		startPage();
        List<StatisticsApplication> list = statisticsApplicationService.selectStatisticsApplicationList(statisticsApplication);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出项目应用列表
	 */
	@RequiresPermissions("statistics:statisticsApplication:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StatisticsApplication statisticsApplication)
    {
    	List<StatisticsApplication> list = statisticsApplicationService.selectStatisticsApplicationList(statisticsApplication);
        ExcelUtil<StatisticsApplication> util = new ExcelUtil<StatisticsApplication>(StatisticsApplication.class);
        return util.exportExcel(list, "statisticsApplication");
    }
	@PostMapping("/importData")
	@ResponseBody
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
	{
		ExcelUtil<StatisticsApplication> util = new ExcelUtil<StatisticsApplication>(StatisticsApplication.class);
		List<StatisticsApplication> applicationList = util.importExcel(file.getInputStream());
		for (StatisticsApplication statisticsApplication:applicationList) {
			statisticsApplication.setUnitid(UnitUtils.getUnitId(statisticsApplication.getUnit()));
			statisticsApplicationService.insertStatisticsApplication(statisticsApplication);
		}
		return AjaxResult.success("导入成功");
	}
	/**
	 * 新增项目应用
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存项目应用
	 */
	@RequiresPermissions("statistics:statisticsApplication:add")
	@Log(title = "项目应用", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(StatisticsApplication statisticsApplication)
	{
		statisticsApplication.setUnit(UnitUtils.unitById(statisticsApplication.getUnitid()));
		return toAjax(statisticsApplicationService.insertStatisticsApplication(statisticsApplication));
	}

	/**
	 * 修改项目应用
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		StatisticsApplication statisticsApplication = statisticsApplicationService.selectStatisticsApplicationById(id);
		mmap.put("statisticsApplication", statisticsApplication);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存项目应用
	 */
	@RequiresPermissions("statistics:statisticsApplication:edit")
	@Log(title = "项目应用", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(StatisticsApplication statisticsApplication)
	{
		statisticsApplication.setUnit(UnitUtils.unitById(statisticsApplication.getUnitid()));
		return toAjax(statisticsApplicationService.updateStatisticsApplication(statisticsApplication));
	}
	
	/**
	 * 删除项目应用
	 */
	@RequiresPermissions("statistics:statisticsApplication:remove")
	@Log(title = "项目应用", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(statisticsApplicationService.deleteStatisticsApplicationByIds(ids));
	}
	
}
